//关于表单验证规则的封装

class ProofRules {
  //验证手机规则
  static checkMobile = (rules, value, callback) => {
    const regPhoneNumber = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
    if (regPhoneNumber.test(value)) {
      return callback()
    } else {
      callback(new Error('请填写合法的手机号码'))
    }
  }

  //验证手机的完全封装
  static MobileMethod(title){
    return [
      this.requiredField(title),
      {
        validator: this.checkMobile,
        trigger: 'blur'
      }
    ]
  }

  //验证价格的正则
  static checkPrice = (rules, value, callback) => {
    const regPrice = /(^[1-9]\d*.\d{1,2}$)|(^0.\d{1,2}$)|(^[1-9]\d*$)/
    if (regPrice.test(value)) {
      return callback()
    } else {
      callback(new Error('请正确填写价格，要求最多为两位小数且不能为0'))
    }
  }

  //基础课程价格的完全封装
  static PriceMethod (title) {
    return [
      this.requiredField(title),
      {
        validator: this.checkPrice,
        trigger: 'blur'
      }
    ]
  }

  //验证人数的正则---->0且为整数
  static checkAccommodate = (rules, value, callback) => {
    const regAccommodate = /^[1-9]\d*$/
    if (regAccommodate.test(value)) {
      return callback()
    } else {
      callback(new Error('请正确填入人数，必须为正整数且不能为0'))
    }
  }

  //人数的完整封装
  static accommodateMethod (title) {
    return [
      this.requiredField(title),
      {
        validator: this.checkAccommodate,
        trigger: 'blur'
      }
    ]
  }

  //验证课时的正则
  static checkClassHours = (rules, value, callback) => {
    const regAccommodate = /^[1-9]\d*$/
    if (regAccommodate.test(value)) {
      return callback()
    } else {
      callback(new Error('请正确填入课时时长，必须为正整数且不能为0'))
    }
  }

  //对于时长验证的完全封装
  static classHoursMethod(title){
   return[
     this.requiredField(title),
     {
       validator: this.checkClassHours,
       trigger: 'blur'
     }
   ]
  }

  //验证身份证的正则
  static checkIdentity=(rules,value,callback)=>{
    const regAccommodate=/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    if (regAccommodate.test(value)) {
      return callback()
    } else {
      callback(new Error('请合法的身份证号码'))
    }
  }

  //身份证号码的完全封装
  static identityMethod(title){
    return[
      this.requiredField(title),
      {
        validator: this.checkIdentity,
        trigger: 'blur'
      }
    ]
  }

  //封装必填项----返回的是个对象
  static requiredField (title) {
    return {
      required: true,
      message: '请输入' + title,
      trigger: 'blur'
    }
  }

  //封装必填项的长度
  static requiredLength (min, max) {
    return {
      min: min,
      max: max,
      message: '长度在' + min + '到' + max + '个字符',
      trigger: 'blur'
    }
  }

  //封装一个完整的，验证长度和必填项
  static requiredFieldAndLength (title, min, max) {
    return [
      this.requiredField(title),
      this.requiredLength(min, max)
    ]
  }

}

export default ProofRules
